package com.hxzy.controller;

import com.github.tobato.fastdfs.FdfsClientConstants;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.vo.R;
import com.hxzy.exception.ValidatorException;
import com.hxzy.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@Api(tags = "文件上传API")
@RestController
@RequestMapping(value = "/api/upload")
public class FileUploadController extends BaseController {

   @Autowired
    private FileUploadService fastDfsFileUploadServiceImpl;



    /**
     * 缩略图宽
     */
    @Value(value = "${fdfs.thumb-image.width}")
    private String thumbImageWidth;

    /**
     * 缩略图高
     */
    @Value(value = "${fdfs.thumb-image.height}")
    private String thumbImageHeight;



    @ApiOperation(value = "图片上传")
    @PostMapping(value = "/image")
    public R uploadImage(@RequestPart @ApiParam(name = "file",value = "file", required = true)MultipartFile file) throws IOException {
        // 文件 没有内容的
        if(file.isEmpty()){
            throw new ValidatorException(AckCode.FILE_NOT_EMPTY);
        }

        // 支持的图片格式包括"JPG", "JPEG", "PNG", "GIF", "BMP",
        List<String> imageSet= Arrays.asList(FdfsClientConstants.SUPPORT_IMAGE_TYPE);
        //原始名称
        String originalFilename = file.getOriginalFilename();
        //得到扩展名
        String fileExtName= originalFilename.substring( originalFilename.lastIndexOf(".")+1).toUpperCase(Locale.ROOT);
        //不在里面不支持
        if(!imageSet.contains(fileExtName)){
            throw new ValidatorException(AckCode.UPLOAD_TYPE_ERROR_IMAGE);
        }

        //检查文件大小<1MB
        if(file.getSize()> 1024L*1024*1){
            throw new ValidatorException(AckCode.UPLOAD_IMAGE_SIZE_LIMIT);
        }

        //group1/M00/00/00/wKgatGNXiQCALPqFAAGLNm78RqQ307.png
        String fullPath=this.fastDfsFileUploadServiceImpl.uploadImage(file);

        Map<String,String> map=new HashMap<>();
        map.put("url",fullPath);
        map.put("fileName",originalFilename);

        return R.okHasData(map);
    }


    @ApiOperation(value = "图片上传并生成缩略图")
    @PostMapping(value = "/imagecrt")
    public R uploadImageAndCrtThumbImage(@RequestPart @ApiParam(name = "file",value = "file", required = true)MultipartFile file) throws IOException {
        // 文件 没有内容的
        if(file.isEmpty()){
            throw new ValidatorException(AckCode.FILE_NOT_EMPTY);
        }

        // 支持的图片格式包括"JPG", "JPEG", "PNG", "GIF", "BMP",
        List<String> imageSet= Arrays.asList(FdfsClientConstants.SUPPORT_IMAGE_TYPE);
        //原始名称
        String originalFilename = file.getOriginalFilename();
        //得到扩展名
        String fileExtName= originalFilename.substring( originalFilename.lastIndexOf(".")+1).toUpperCase(Locale.ROOT);
        //不在里面不支持
        if(!imageSet.contains(fileExtName)){
            throw new ValidatorException(AckCode.UPLOAD_TYPE_ERROR_IMAGE);
        }

        //检查文件大小<1MB
        if(file.getSize()> 1024L*1024*1){
            throw new ValidatorException(AckCode.UPLOAD_IMAGE_SIZE_LIMIT);
        }

        //group1/M00/00/00/wKgatGNXiQCALPqFAAGLNm78RqQ307.png
        String fullPath=this.fastDfsFileUploadServiceImpl.uploadImageAndCrtThumbImage(file);

        // 生成缩略首的URL
        StringBuffer  sb=new StringBuffer();

        sb.append(fullPath.substring(0,fullPath.lastIndexOf(".")));
        sb.append("_").append(this.thumbImageWidth).append("x").append(this.thumbImageHeight);
        sb.append(fullPath.substring(fullPath.lastIndexOf(".")));


        Map<String,String> map=new HashMap<>();
        map.put("url",fullPath);
        map.put("url_crt",sb.toString());
        map.put("fileName",originalFilename);
        return R.okHasData(map);
    }
}
